<?php
namespace app\models;

use fate\db\Db;
use app\libs\Sql;

/**
 * 接口层
 */
abstract class IModel {
    public $db = null;
    public $table = '';

    public function __construct() {
        $this->db = Db::instance('main');
    }

    /**
     * get one column
     *
     * @param string $pkid
     * @return string 字段值
     */
    public function getColumn($field, $pkid) {
        $sql = 'SELECT ' . $field . ' FROM ' . $this->table . ' WHERE id=?';
        $col = $this->db->prepareStatement($sql)->bindValues([$pkid])->queryColumn();

        if(!$col) {
            return '';
        }

        return $col;
    }

    /**
     * get one record
     *
     * @param string $pkid
     * @return array|null 一维数组
     */
    public function getOne($fields, $pkid) {
        $sql = 'SELECT ' . $fields . ' FROM ' . $this->table . ' WHERE id=?';
        $data = $this->db->prepareStatement($sql)->bindValues([$pkid])->queryOne();

        if(!$data) {
            return null;
        }

        return $data;
    }

    /**
     * getone by condition
     *
     * @param string $where
     * @param string $fields
     * @return array|null 一维数组
     */
    public function getOneByCondition($fields, $where, $orderBy = '') {
        $ret = $this->db->createQuery()
            ->select($fields)
            ->from($this->table)
            ->where($where);

        if('' !== $orderBy) {
            $ret = $ret->orderBy($orderBy);
        }

        $data = $ret->getOne();

        if(!$data) {
            return null;
        }

        return $data;
    }

    /**
     * get by condition
     *
     * @param string $where
     * @param string $fields
     * @return array|null 多维数组
     */
    public function getListByCondition($fields, $where, $limit = 100, $orderBy = '') {
        $ret = $this->db->createQuery()
            ->select($fields)
            ->from($this->table)
            ->where($where)
            ->limit($limit);

        if('' !== $orderBy) {
            $ret = $ret->orderBy($orderBy);
        }

        $data = $ret->getAll();

        if(!$data) {
            return null;
        }

        return $data;
    }

    /**
     * get by page
     *
     * @param string $where
     * @param number $page
     * @param number $pageSize
     * @param string $fields
     * @return array|null 多维数组
     */
    public function getListByPage($fields, $where, $page, $pageSize) {
        $limit = ($page - 1) * $pageSize;
        $sql = Sql::buildSelectSql($this->table, $fields, $where, $limit, $pageSize);

        $data = $this->db->prepareSql($sql)->queryAll();

        if(!$data) {
            return null;
        }

        return $data;
    }

    /**
     * 插入记录
     *
     * @param array $data
     */
    public function insert($data) {
        $k = array_keys($data);
        $v = array_values($data);

        // insert into t(title, post_time) value(?, ?)
        $fields = implode(',', $k);
        $valuesHolder = '';
        for($i=0,$len=count($k); $i<$len; $i++) {
            $valuesHolder .= '?,';
        }
        $valuesHolder = rtrim($valuesHolder, ',');

        // insert into t(age) values(1);
        $sql = 'INSERT INTO ' . $this->table . '('. $fields .') VALUES('. $valuesHolder .')';
        $this->db->prepareStatement($sql)->bindValues($v)->execute();
    }

    /**
     * 删除
     *
     * @param number $pkid
     */
    public function delete($pkid) {
        $sql = 'DELETE from ' . $this->table . ' WHERE id=?';
        $this->db->prepareStatement($sql)->bindValues([$pkid])->execute();
    }

    /**
     * 删除
     */
    public function deleteByCondition($condition) {
        $sql = 'DELETE from ' . $this->table . ' WHERE ' . $condition;
        $this->db->prepareSql($sql)->execute();
    }

    /**
     * 修改记录
     */
    public function update($pkid, $data) {
        $v = array_values($data);
        $v[] = $pkid;

        $ret = '';
        foreach($data as $key => $val) {
            $ret = $ret . $key . '=?,';
        }
        $ret = rtrim($ret, ',');

        // update t set age=1 where id=1
        $sql = 'UPDATE ' . $this->table . ' set ' . $ret . ' where id=?';

        $this->db->prepareStatement($sql)->bindValues($v)->execute();
    }

    /**
     * 修改记录
     */
    public function updateByCondition($data, $where) {
        $v = array_values($data);

        $ret = '';
        foreach($data as $key => $val) {
            $ret = $ret . $key . '=?,';
        }
        $ret = rtrim($ret, ',');

        // update t set age=1 where id=1
        $sql = 'UPDATE ' . $this->table . ' set ' . $ret . ' where ' . $where;

        $this->db->prepareStatement($sql)->bindValues($v)->execute();
    }

    /**
     * 统计
     *
     * @param string $condition
     */
    public function count($condition, $pk = 'id') {
        return $this->db->createQuery()->from($this->table)->where($condition)->count($pk);
    }
}
